package mtm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Vars {

    private static String Version = "MTM v0.01-00a";

    public static String getVersion() {
        return Version;
    }
    // Properties
    /*
     * PluginsToLoad - ";"-separated string of plugins
     * http.proxyHost - null for none
     * http.proxyPort - null for none
     */
    public static Properties prop = new Properties();
    public static String PropFileName = "";
    public static String PropPath = "";
    public static String PluginsPath = "";
    public static String DataPath = "";

    public static void SaveProperties() {
        try {
            prop.storeToXML(new FileOutputStream(PropFileName, false), null);
        } catch (IOException ex) {
            Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void LoadProperties() {
        try {
            Vars.prop.loadFromXML(new FileInputStream(PropFileName));
        } catch (IOException ex) {
            Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Plugins
    private static ArrayList<mtm.PluginAPI> Plugins = new ArrayList<PluginAPI>();
    private static ArrayList<mtm.PluginAPI.DataObject> PluginObjecs = new ArrayList<PluginAPI.DataObject>();

    public static void LoadPlugins() {
        Vars.Plugins.clear();
        String[] pnames;
        pnames = Vars.prop.getProperty("PluginsToLoad").split(";");
        for (String pname : pnames) {
            File jf = new File(Vars.PluginsPath + "/" + pname + ".jar");
            Class pluginClass = null;
            URLClassLoader classLoader;
            try {
                classLoader = new URLClassLoader(new URL[]{jf.toURI().toURL()});
                pluginClass = classLoader.loadClass(pname + ".PluginMain");
            } catch (MalformedURLException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            mtm.PluginAPI plugin = null;
            try {
                plugin = (PluginAPI) pluginClass.newInstance();
            } catch (InstantiationException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            Vars.Plugins.add(plugin);
            System.out.println("Plugin " + plugin.getPluginName() + " loaded");
        }
    }

    public static int getPluginIndex(String NameOrID) {
        Integer index = 0;
        while (index < Vars.Plugins.size()) {
            mtm.PluginAPI plugin = Vars.Plugins.get(index);
            if (plugin.getPluginID().equals(NameOrID) || plugin.getPluginName().equals(NameOrID)) {
                return index;
            }
        }
        return -1;
    }

    public static mtm.PluginAPI getPlugin(String NameOrID) {
        for (mtm.PluginAPI plugin : Vars.Plugins) {
            if (plugin.getPluginID().equals(NameOrID) || plugin.getPluginName().equals(NameOrID)) {
                return plugin;
            }
        }
        return null;
    }

    public static mtm.PluginAPI[] getPlugins() {
        mtm.PluginAPI[] plugins = new PluginAPI[Vars.Plugins.size()];
        for (Integer index = 0; index < Vars.Plugins.size(); index++) {
            plugins[index] = Vars.Plugins.get(index);
        }
        return plugins;
    }

    public static mtm.PluginAPI.DataObject[] getPluginObjects() {
        mtm.PluginAPI.DataObject[] pluginobjects = new mtm.PluginAPI.DataObject[Vars.PluginObjecs.size()];
        for (Integer index = 0; index < Vars.PluginObjecs.size(); index++) {
            pluginobjects[index] = Vars.PluginObjecs.get(index);
        }
        return pluginobjects;
    }

    public static void addPluginObject(mtm.PluginAPI.DataObject pluginobject) {
        Vars.PluginObjecs.add(pluginobject);
    }
    // Util

    public static String getHexString(String str) {
        String hexstr = "";
        int i = 0;
        while (i < str.length()) {
            Integer ic = str.codePointAt(i);
            hexstr = hexstr + Integer.toHexString(ic);
            hexstr = hexstr + "|";
            i++;
        }
        return hexstr;
    }

    public static String getStringFromHex(String hexstr) {
        if (hexstr == null) {
            return "";
        }
        String str = "";
        String s = "";
        Integer ci = 0;
        char c = (char) 0;
        int i = 0;
        while (i < hexstr.length()) {
            if (!hexstr.substring(i, i + 1).equals("|")) {
                s = s + hexstr.substring(i, i + 1);
                i++;
            } else {
                try {
                    ci = Integer.parseInt(s, 16);
                } catch (NumberFormatException ex) {
                    Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
                    return "Error parsing HEX";
                }
                c = (char) ci.intValue();
                str = str + c;
                s = "";
                i++;
            }
        }
        return str;
    }

    public static Boolean CopyFile(String SourceFile, String DestFile) {
        FileChannel ic = null;
        try {
            ic = new FileInputStream(SourceFile).getChannel();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        FileChannel oc = null;
        try {
            oc = new FileOutputStream(DestFile).getChannel();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        try {
            ic.transferTo(0, ic.size(), oc);
            ic.close();
            oc.close();
        } catch (IOException ex) {
            Logger.getLogger(Vars.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }
}
